﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Xaml.Schema;
using WindowsFormsApp2.ModbusRtuSet;
using YJYpublicClass;

namespace WindowsFormsApp2.ModbusRtuSet
{
    public partial class Frm_Rtu : Form
    {
        NModbus4Rtu tempClient = null;

        public Frm_Rtu(NModbus4Rtu rtuClient)
        {
            InitializeComponent();
            tempClient = rtuClient;
        }

        private void Frm_Rtu_Load(object sender, EventArgs e)
        {
            this.propertyGrid1.SelectedObject = tempClient.rtuConfig;
            this.timer1.Enabled = true;
        }

        private void uiButton1_Click(object sender, EventArgs e)
        {
            if (tempClient.Saveconfig().State == Funstate.OK)
            {
                MessageBox.Show("保存成功");
            }
            else
            {
                MessageBox.Show("保存失败");
            }
        }






        private void uiBtn_CoilWrite_Click(object sender, EventArgs e)
        {
            //Boolean value = true;
            //if (string.Equals(this.uiTextB_valueWrite.Text, "true", StringComparison.InvariantCultureIgnoreCase))
            //{
            //    value = true;
            //}
            //else if (string.Equals(this.uiTextB_valueWrite.Text, "1", StringComparison.InvariantCultureIgnoreCase))
            //{
            //    value = true;
            //}
            //else
            //{
            //    value = false;
            //}
            //if (tempClient.WriteCoil(this.uiTextB_adressWrite.Text, value))
            //{
            //    uiRichTextBox1.AppendText("写入成功" + "\r\n");
            //}
            //else
            //{
            //    uiRichTextBox1.AppendText("写入失败" + "\r\n");
            //}
        }

        private void uiBtn_DoubleWrite_Click(object sender, EventArgs e)
        {
            //double value = Convert.ToDouble(this.uiTextB_valueWrite.Text);
            //if (tempClient.WriteDouble(this.uiTextB_adressWrite.Text, value))
            //{
            //    uiRichTextBox1.AppendText("写入成功" + "\r\n");
            //}
            //else
            //{
            //    uiRichTextBox1.AppendText("写入失败" + "\r\n");
            //}
        }

        private void uiBtn_ShortWrite_Click(object sender, EventArgs e)
        {
            ushort value = Convert.ToUInt16(this.uiTextB_valueWrite.Text);
            if (tempClient.WriteShort(this.uiTextB_adressWrite.Text, value))
            {
                uiRichTextBox1.AppendText("写入成功" + "\r\n");
            }
            else
            {
                uiRichTextBox1.AppendText("写入失败" + "\r\n");
            }
        }





        private void uiBtn_CoilRead_Click(object sender, EventArgs e)
        {
            //bool value = false;
            //tempClient.ReadCoil(this.uiTextB_adressRead.Text, out value);
            //uiRichTextBox1.AppendText("读取值：" + value + "\r\n");
            //this.uiTextB_valueRead.Text = value.ToString();
        }

        private void uiBtn_DoubleRead_Click(object sender, EventArgs e)
        {
            //double value = 0;
            //tempClient.ReadDouble(this.uiTextB_adressRead.Text, out value);
            //uiRichTextBox1.AppendText("读取值：" + value + "\r\n");
            //this.uiTextB_valueRead.Text = value.ToString();
        }

        private void uiBtn_ShortRead_Click(object sender, EventArgs e)
        {
            ushort value = 0;
            tempClient.ReadShort(this.uiTextB_adressRead.Text, out value);
            uiRichTextBox1.AppendText("读取值：" + value + "\r\n");
            this.uiTextB_valueRead.Text = value.ToString();
        }

        private void Btn_Connect_Click(object sender, EventArgs e)
        {
            tempClient.Connect();
        }

        private void Btn_DisConnect_Click(object sender, EventArgs e)
        {
            tempClient.Close();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (tempClient.IsConnect)
            {
                Btn_Connect.Enabled = false;
                Btn_DisConnect.Enabled = true;
            }
            else
            {
                Btn_Connect.Enabled = true;
                Btn_DisConnect.Enabled = false;
            }
        }
    }
}
